<template>
	<view>
		<view class='mask' v-if='isShowAuth && code' @click='close'></view>
		<view class='Popup' v-if='isShowAuth && code'>
			<view class="head">
				<view class="logo-auth">
					<image class="image" :src='routine_logo' mode="aspectFit"></image>
				</view>
				<text class='tip'>捞鱼客源接单 申请</text>
			</view>
			<view class="body">
				<view class="tip_title">{{title}}</view>
				<view class="tip_info">{{info}}</view>
			</view>
			<view class='bottom flex'>
				<text class='item' @click='close'>随便逛逛</text>
				<!-- #ifdef MP -->
				<button class="item grant" hover-class="none"
					@tap="getUserProfile"><text class="text">去授权</text></button>
				<!-- #endif -->
				<!-- #ifdef APP-PLUS -->
				<button class="item grant" @tap="toWecahtAuth">
					<text class="text">去登录</text>
				</button>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<button class="item grant" @tap="toWecahtAuth">
					<text v-if="isWeixin" class="text">去授权</text>
					<text v-else class="text">去登录</text>
				</button>
				<!-- #endif -->
			</view>
		</view>
		<!-- #ifdef MP -->
		<editUserModal :isShow="editModal" @closeEdit="closeEdit" @editSuccess="editSuccess"></editUserModal>
		<privacyMenu ref="privacyMenuPopup"/>
		<!-- #endif -->
	</view>
</template>
<script>
	 
	const app = getApp();
	import Cache from '../utils/cache';
	import {
		getLogo, commonAuth
	} from '../api/public';
	import { LOGO_URL, USER_INFO, EXPIRES_TIME} from '../config/cache';
	import { mapGetters } from 'vuex';
	import Routine from '../libs/routine';
	import { configMap } from '@/utils/index';
	import Auth from '../libs/wechat';
	import { toLogin } from '../libs/login';
	import { saveUserAddress } from '../api/user.js';
	// #ifdef MP
	import editUserModal from '@/components/eidtUserModal/index.vue'
	import privacyMenu from '@/components/privacyMenu.vue';
	// #endif
	export default {
		name: 'Authorize',
		props: {
			isAuto: {
				type: Boolean,
				default: true
			},
			isGoIndex: {
				type: Boolean,
				default: true
			},
			isShowAuth: {
				type: Boolean,
				default: false
			}
		},
		components: {
			// #ifdef MP
			editUserModal,
			privacyMenu
			// #endif
		},
		data() {
			return {
				title: '用户登录',
				info: '请登录，将为您提供更好的服务！',
				//#ifdef H5
				isWeixin: this.$wechat.isWeixin(),
				//#endif
				//#ifdef MP
				title: '授权提醒',
				info: '请授权头像等信息，以便为您提供更好的服务！',
				//#endif
				canUseGetUserProfile: false,
				code: null,
				top: 0,
				mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false,
				editModal: false, // 编辑头像信息
			}
		},
		computed: {
			...mapGetters(['isLogin', 'userInfo', 'viewColor']),
			...configMap(['routine_logo'])
		},
		watch: {
			isLogin(n) {
				n === true && this.$emit('onLoadFun', this.userInfo);
			},
			isShowAuth(n) {
				this.getCode(this.isShowAuth)
			}
		},
		created() {
			// console.log('title'+this.title)
			this.top = uni.getSystemInfoSync().windowHeight/2-70
			if (wx.getUserProfile) {
				this.canUseGetUserProfile = true
			}
			this.setAuthStatus();
			this.getCode(this.isShowAuth)
		},
		methods: {
			// #ifdef MP
			editSuccess() {
				this.editModal = false
				this.$emit('onLoadFun', this.userInfo);
			},
			closeEdit() {
				this.editModal = false
			},
			// #endif
			setAuthStatus() {
				//#ifdef MP
				Routine.authorize().then(res => {
					if (res.islogin === false)
						this.$emit('onLoadFun', this.userInfo);
				}).catch(res => {
					if (this.isAuto)
						this.$emit('authColse', true);
				})
				//#endif
			},
			getCode(n){
				// #ifdef MP
					if (n) {
						uni.showLoading({
							title: '正在登录中'
						});
						Routine.getCode().then(code => {
							uni.hideLoading();
							this.code = code;
						}).catch(e => {
							uni.hideLoading();
							uni.showToast({
								title: '登录失败',
								duration: 2000
							});
						})
					} else {
						this.code = null;
					}
				// #endif
				// #ifndef MP
					if(n){
						this.code = 1;
					}
				// #endif
			},
			toWecahtAuth(){
				toLogin(true);
			},
			getUserProfile() {
				let self = this;
				// 小程序隐私弹窗
				if(app.globalData.showPrivacy) {
					return this.$refs.privacyMenuPopup.handleDisagreeOpen()
				}
				Routine.getUserProfile()
					.then(res => {
						let userInfo = res.userInfo;
						userInfo.code = this.code;
						userInfo.spread = app.globalData.spid; //获取推广人ID
						userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
						if(!Cache.has('engagedIndustry')) return uni.navigateTo({url:'/pages/guide/index'});
						userInfo.engaged_industry = Cache.get('engagedIndustry'); // 我从事行业标签
						userInfo.customer_industry = Cache.has('customerIndustry') ? Cache.get('customerIndustry') : Cache.get('engagedIndustry'); // 目标客户标签
						commonAuth({
							auth: {
							type:'routine',
							auth: userInfo
						}
						}).then(res=>{
							if(res.data.status == 200){
								let time = res.data.result.expires_time - Cache.time();
								self.$store.commit('UPDATE_USERINFO', res.data.result.user);
								self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
								self.$store.commit('SETUID', res.data.result.user.uid);
								Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
								Cache.set(USER_INFO,res.data.result.user,time);
								this.$emit('onLoadFun', res.data.result.user);
								if(res.data.result.user.isNew && this.mp_is_new){
									// this.editModal = true;
								}
								// 登陆成功修改用户定位信息
								if(Cache.has('userAddress')){
									let userAddress = Cache.get('userAddress', true);// 获取用户定位信息
									saveUserAddress(userAddress)
								}
							}else{
								uni.setStorageSync('auth_token',res.data.result.key);
								return uni.navigateTo({
									url:'/pages/users/login/index'
								})
							}
						}).catch(res => {
							uni.hideLoading();
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000,
								
							});
						});
					})
					.catch(res => {
						uni.hideLoading();
					});
			},
			close() {
				let pages = getCurrentPages(),
					currPage = pages[pages.length - 1];
					this.$emit('authColse', false);
				// if (this.isGoIndex) {
				// 	uni.switchTab({
				// 		url: '/pages/index/index'
				// 	});
				// } else {
				// 	this.$emit('authColse', false);
				// }
			},
		}
	}
</script>

<style scoped lang='scss'>
	.Popup {
		flex: 1;
		align-items: center;
		justify-content: center;
		width: 100%;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 1000;
		border-radius: 15rpx 15rpx 0 0;
		padding: 30rpx 40rpx 60rpx;
		.head {
			display: flex;
			align-items: center;
			background-color: #fff;
			.logo-auth {
				width: 30rpx;
				height: 30rpx;
				border-radius: 50%;
				image {
					width: 30rpx;
					height: 30rpx;
					border-radius: 50%;
				}
			}
			.tip {
				margin-left: 20rpx;
				font-size: 34rpx;
				font-weight: 500;
				color: #171717;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
		.body {
			margin-top: 50rpx;
			.tip_title {
				color: #171717;
				font-size: 38rpx;
				font-weight: 500;
			}
			.tip_info {
				margin-top: 10rpx;
				color: #767676;
			}
		}
	}
	.Popup .bottom{
		justify-content: center;
	}
	.Popup .bottom .item {
		width: 260rpx;
		height: 80rpx;
		background-color: #F2F2F2;
		text-align: center;
		line-height: 80rpx;
		margin-top: 95rpx;
		font-size: 38rpx;
		color: #06AE56;
		border-radius: 5rpx;
		.text{
			font-size: 38rpx;
			color: #fff;
		}
	}
	.flex {
		display: flex;
		flex-direction: row;
	}
	.Popup .bottom .item.grant {	
		margin-left: 30rpx;
		background-color: #07C160;
		.text{
			font-size: 38rpx;
			color: #fff;
		}
	}
	.mask {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.65);
		z-index: 99;
	}
</style>
